package com.bridge.common.weight;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bridge.common.R;

/**
 * 公用标题栏
 */
public class TitleView extends ConstraintLayout {

    public RelativeLayout mLayoutHeader;
    public ImageButton mBtnBack;
    public TextView mTvTitleText;
    public ImageButton mBtnRight;
    public TextView mTvRightText;
    public Context mContext;

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_title, this);
        mLayoutHeader = (RelativeLayout) findViewById(R.id.cl_header_title);
        mBtnBack = (ImageButton) findViewById(R.id.btn_back);
        mTvTitleText = (TextView) findViewById(R.id.tv_title_text);
        mBtnRight = (ImageButton) findViewById(R.id.rightBtn);
        mTvRightText = (TextView) findViewById(R.id.rightTxt);

        initAttrs(attrs);
    }

    /**
     * 解析赋值属性
     *
     * @param attrs
     */
    private void initAttrs(AttributeSet attrs) {
        TypedArray ta = this.getContext().obtainStyledAttributes(attrs, R.styleable.TitleView);
        if (ta != null) {

            //标题栏背景色
            Drawable headerBg = ta.getDrawable(R.styleable.TitleView_header_background);
            if (null != headerBg)
                mLayoutHeader.setBackground(headerBg);

            //返回按钮是否显示
            boolean backVisable = ta.getBoolean(R.styleable.TitleView_backBtnVisible, true);
            mBtnBack.setVisibility(backVisable ? VISIBLE : INVISIBLE);
            if (backVisable) {
                mBtnBack.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mContext instanceof Activity) {
                            ((Activity) mContext).finish();
                        }
                    }
                });
            }

            //标题
            String title = ta.getString(R.styleable.TitleView_titleText);
            if (!TextUtils.isEmpty(title))
                mTvTitleText.setText(title);

            float titleSize = ta.getDimension(R.styleable.TitleView_titleTextSize, 18);
            mTvTitleText.setTextSize(titleSize);

            int titleColor = ta.getColor(R.styleable.TitleView_titleTextColor, getResources().getColor(R.color.white));
            mTvTitleText.setTextColor(titleColor);

            //右边按钮是否显示
            boolean visable = ta.getBoolean(R.styleable.TitleView_rightBtnVisible, false);
            mBtnRight.setVisibility(visable ? VISIBLE : INVISIBLE);

            //右侧按钮
            int rightImageRes = ta.getResourceId(R.styleable.TitleView_rightBtnImage, 0);
            if (rightImageRes != 0) {
                mBtnRight.setVisibility(View.VISIBLE);
                mBtnRight.setImageResource(rightImageRes);
            }

            //右侧文字
            String rightText = ta.getString(R.styleable.TitleView_rightText);
            if (!TextUtils.isEmpty(rightText))
                mTvRightText.setText(rightText);

            int rightTextColor = ta.getColor(R.styleable.TitleView_rightTextColor, getResources().getColor(R.color.white));
            mTvRightText.setTextColor(rightTextColor);

            ta.recycle();
        }

    }

    /**********控件动态设置的方法******************/
    public TitleView setBackVisibility(boolean b) {
        mBtnBack.setVisibility(b ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public TitleView setBackOnClickListener(OnClickListener listener) {
        mBtnBack.setOnClickListener(listener);
        return this;
    }

    public TitleView setTitle(String txt) {
        mTvTitleText.setText(txt);
        return this;
    }

    public TitleView setTitle(int resId) {
        mTvTitleText.setText(resId);
        return this;
    }

    public TitleView setRightResource(int resId) {
        mBtnRight.setImageResource(resId);
        return this;
    }
    public TitleView setRightTextColor(int color) {
        mTvRightText.setTextColor(color);
        return this;
    }
    public TitleView setRightVisibility(boolean b) {
        mBtnRight.setVisibility(b ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public TitleView setRightOnClickListener(OnClickListener listener) {
        mBtnRight.setOnClickListener(listener);
        return this;
    }

    public TitleView setRightTextOnClickListener(OnClickListener listener) {
        mTvRightText.setOnClickListener(listener);
        return this;
    }

    public TitleView setRightText(int txtId) {
        mTvRightText.setText(txtId);
        return this;
    }

    public TitleView setRightTextVisible(boolean b) {
        mTvRightText.setVisibility(b ? View.VISIBLE : View.INVISIBLE);
        return this;
    }


    public TitleView setRightText(String strText) {
        mTvRightText.setText(strText);
        return this;
    }
    public TitleView setRightTextViewOnClickListener(OnClickListener listener) {
        mTvRightText.setOnClickListener(listener);
        return this;
    }
}
